﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YJKStairs.CommonUnit
{
	public class DimesionTypeBuilder
	{
		private void InitDimTypeData()
		{
			foreach (DimensionType dimensionType in DimesionTypeBuilder.StairsDimensionStyleMgr.GetDimensionTypeByStyleType(0, this.m_rvtDoc))
			{
				string name = dimensionType.Name;
				if (!this.m_arDimType.ContainsKey(name))
				{
					this.m_arDimType.Add(name, dimensionType);
				}
				else
				{
					this.m_arDimType[name] = dimensionType;
				}
			}
		}

		private void SetElementStringParamValue(Element ele, string sParamName, string sValue)
		{
			Parameter parameter = ele.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly && parameter.AsString() != sValue)
			{
				parameter.Set(sValue);
			}
		}

		private void SetElementDoubleParamValue(Element ele, string sParamName, double dValue)
		{
			Parameter parameter = ele.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly && parameter.AsDouble() != dValue)
			{
				parameter.Set(dValue);
			}
		}

		private void SetElementIntParam(Element ele, string sParamName, int nValue)
		{
			Parameter parameter = ele.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly && parameter.AsInteger() != nValue)
			{
				parameter.Set(nValue);
			}
		}

		private void SetElementIdParam(Element ele, string sParamName, ElementId id)
		{
			Parameter parameter = ele.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly && parameter.AsElementId() != id && null != id)
			{
				parameter.Set(id);
			}
		}

		private ElementId GetDimLeaderHeadTypeId(string sTypeName)
		{
			ElementId result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_rvtDoc);
            filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.ElementType));
			Element element2 = (from element in filteredElementCollector
			where element.Name.Equals(sTypeName)
			select element).First<Element>();
			if (element2 != null)
			{
				result = element2.Id;
			}
			return result;
		}

		public DimesionTypeBuilder(Autodesk.Revit.DB.Document doc)
		{
			this.m_rvtDoc = doc;
			this.InitDimTypeData();
		}

		public DimensionType CreateStairsRunDimType(string sTypeName)
		{
			DimensionType result = null;
			if (this.m_arDimType.Count > 0 && !string.IsNullOrEmpty(sTypeName))
			{
				if (this.m_arDimType.Keys.Contains(sTypeName))
				{
					result = this.m_arDimType[sTypeName];
				}
				else
				{
					DimensionType dimensionType = this.m_arDimType.Values.First<DimensionType>();
					if (dimensionType != null)
					{
						DimensionType dimensionType2 = dimensionType.Duplicate(sTypeName) as DimensionType;
						this.SetElementIntParam(dimensionType2, "文本移动时显示引线", 1);
						this.SetElementIdParam(dimensionType2, "记号", this.GetDimLeaderHeadTypeId("对角线 3mm"));
						this.SetElementIntParam(dimensionType2, "尺寸界线控制点", 1);
						this.SetElementDoubleParamValue(dimensionType2, "尺寸界线长度", Common.MMToFeet(8.0));
						this.SetElementDoubleParamValue(dimensionType2, "尺寸界线延伸", Common.MMToFeet(1.0));
						this.SetElementIntParam(dimensionType2, "尺寸界线控制点", 65820);
						this.SetElementDoubleParamValue(dimensionType2, "文字大小", Common.MMToFeet(3.5));
						this.SetElementIntParam(dimensionType2, "文字背景", 1);
						this.SetElementDoubleParamValue(dimensionType2, "文字偏移", Common.MMToFeet(0.0));
						this.SetElementStringParamValue(dimensionType2, "文字字体", "宋体");
						result = dimensionType2;
					}
				}
			}
			return result;
		}

		private Dictionary<string, DimensionType> m_arDimType = new Dictionary<string, DimensionType>();

		private Document m_rvtDoc;

		private class StairsDimensionStyleMgr
		{
			private static void GetCurDocAllDimStyleType(Autodesk.Revit.DB.Document doc)
			{
				if (DimesionTypeBuilder.StairsDimensionStyleMgr.m_dicDimStyleToDimType.Count > 0)
				{
					DimesionTypeBuilder.StairsDimensionStyleMgr.m_dicDimStyleToDimType.Clear();
				}
				foreach (ElementId elementId in new FilteredElementCollector(doc).OfClass(typeof(DimensionType)).ToElementIds().ToList<ElementId>())
				{
					DimensionType dimensionType = doc.GetElement(elementId) as DimensionType;
					if (dimensionType != null)
					{
						DimensionStyleType styleType = dimensionType.StyleType;
						if (dimensionType.ParametersMap != null && dimensionType.ParametersMap.Size != 0)
						{
							if (!DimesionTypeBuilder.StairsDimensionStyleMgr.m_dicDimStyleToDimType.ContainsKey(styleType))
							{
								List<DimensionType> list = new List<DimensionType>();
								list.Add(dimensionType);
								DimesionTypeBuilder.StairsDimensionStyleMgr.m_dicDimStyleToDimType.Add(styleType, list);
							}
							else
							{
								DimesionTypeBuilder.StairsDimensionStyleMgr.m_dicDimStyleToDimType[styleType].Add(dimensionType);
							}
						}
					}
				}
			}

			public static List<DimensionType> GetDimensionTypeByStyleType(DimensionStyleType dimStyleType, Autodesk.Revit.DB.Document doc)
			{
				DimesionTypeBuilder.StairsDimensionStyleMgr.GetCurDocAllDimStyleType(doc);
				return DimesionTypeBuilder.StairsDimensionStyleMgr.m_dicDimStyleToDimType[dimStyleType];
			}

			private static Dictionary<DimensionStyleType, List<DimensionType>> m_dicDimStyleToDimType = new Dictionary<DimensionStyleType, List<DimensionType>>();
		}
	}
}
